package com.yuanqi.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/finance/order-collections")
@CrossOrigin(origins = "*")
public class OrderCollectionController {
    
    @GetMapping
    public ResponseEntity<Map<String, Object>> getOrderCollections(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String orderType,
            @RequestParam(required = false) String orderNumber,
            @RequestParam(required = false) String collectionStatus) {
        
        // 模拟订单回款数据
        List<Map<String, Object>> collections = List.of(
            createCollectionMap(1L, "销售单", "XS001", "2023-01-03", "AAA客户", "夏油杰", "13333333333", 5000.00, 5000.00, "partial", "部分回款", "", "夏油杰", "2024-08-23"),
            createCollectionMap(2L, "销售单", "XS002", "2023-01-05", "BBB客户", "五条悟", "14444444444", 8000.00, 0.00, "paid", "已回款", "已完成回款", "五条悟", "2024-08-24"),
            createCollectionMap(3L, "退货单", "TH001", "2023-01-07", "CCC客户", "伏黑惠", "15555555555", 3000.00, 3000.00, "unpaid", "未回款", "等待回款", "伏黑惠", "2024-08-25")
        );
        
        Map<String, Object> response = new HashMap<>();
        response.put("content", collections);
        response.put("totalElements", 3);
        response.put("totalPages", 1);
        response.put("currentPage", page);
        response.put("size", size);
        
        return ResponseEntity.ok(response);
    }
    
    @PostMapping("/apply")
    public ResponseEntity<Map<String, Object>> applyCollection(@RequestBody Map<String, Object> collectionRequest) {
        // 模拟回款申请处理
        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        response.put("message", "回款申请提交成功");
        response.put("collectionId", System.currentTimeMillis());
        
        return ResponseEntity.ok(response);
    }
    
    @GetMapping("/stats")
    public ResponseEntity<Map<String, Object>> getOrderCollectionStats() {
        Map<String, Object> stats = new HashMap<>();
        stats.put("totalOrders", 200);
        stats.put("unpaidOrders", 60);
        stats.put("partialPaidOrders", 40);
        stats.put("paidOrders", 100);
        stats.put("totalAmount", 2000000.00);
        stats.put("pendingAmount", 600000.00);
        
        return ResponseEntity.ok(stats);
    }
    
    private Map<String, Object> createCollectionMap(Long id, String orderType, String orderNumber, String orderDate,
                                                  String customerName, String responsiblePerson, String contactPhone,
                                                  Double totalAmount, Double pendingAmount, String status,
                                                  String statusText, String remark, String creator, String createTime) {
        Map<String, Object> collection = new HashMap<>();
        collection.put("id", id);
        collection.put("orderType", orderType);
        collection.put("orderNumber", orderNumber);
        collection.put("orderDate", orderDate);
        collection.put("customerName", customerName);
        collection.put("responsiblePerson", responsiblePerson);
        collection.put("contactPhone", contactPhone);
        collection.put("totalAmount", totalAmount);
        collection.put("pendingAmount", pendingAmount);
        collection.put("status", status);
        collection.put("statusText", statusText);
        collection.put("remark", remark);
        collection.put("creator", creator);
        collection.put("createTime", createTime);
        return collection;
    }
}


